package com.taxicall.taxi;

import java.net.InetAddress;

import taxicall.entities.Taxi;
import udptransport.UdpNetworkService;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;

import com.taxicall.taxi.NetworkService.LocalBinder;

/**
 * Abstract class which holds the common properties for all activities. These
 * include the Taxi instance and data relative to the network service.
 * Subclasses however are responsible for initializing the common properties.
 */
public abstract class TaxiNetworkActivity extends Activity {

	protected Taxi thisTaxi;
	protected InetAddress serverIp;

	protected NetworkService service;
	protected UdpNetworkService network;
	protected boolean bound = false;

	protected ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {
			bound = false;
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder iBinder) {
			LocalBinder binder = (LocalBinder) iBinder;
			service = binder.getService();
			bound = true;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = new Intent(this, NetworkService.class);
		bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onStop() {
		super.onStop();
		unbindService(serviceConnection);
	}
}
